package com.java.annotation.ex1;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class DefaultTest {
	public static void main(String args[]){
		DefaultAnnoUse annoUse = new DefaultAnnoUse();
		try {
			// Default注解的处理过程
			// 这里使用反射机制完成默认值的设置
			Field[] fields = annoUse.getClass().getDeclaredFields();
			
			for(Field field : fields) {
				Default annotation = field.getAnnotation(Default.class);
				if(annotation != null) {
					PropertyDescriptor pd = new PropertyDescriptor(field.getName(), DefaultAnnoUse.class);
					Method setterName = pd.getWriteMethod();
					
					if(setterName != null) {
						String value = annotation.value();
						field.setAccessible(true);
						setterName.invoke(annoUse, value);
					}
				}
			}
			
			annoUse.say();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
